const constants   = require('../../data/constants');
const channelModel = require('../../model/channel');
const server      = require('../../sip/server');
const media       = require('./media');

/**
 * 子通道
 */
let Channel = {
    geturl:function(parentid,deviceid,types){
        let str;
        switch(types){
            case 'hls':
                str=constants.zlmedia.remote + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid + '/hls.m3u8';
                break;
            case 'hlsTls':
                str=constants.zlmedia.remoteTls + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid + '/hls.m3u8'
                break;
            case 'rtmp':
                str=constants.zlmedia.rtmp  + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid ;
                break;
            case 'local':
                str=constants.zlmedia.local + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid ;
                break;
            case 'flv':
                str=constants.zlmedia.remote + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid + '.flv';
                break;
            case 'flvTls':
                str=constants.zlmedia.remoteTls + constants.zlmedia.app + '/v_' + parentid + '_' + deviceid + '.flv';
                break;
        }
        return str;
    },
    list:function(req,res){
        channelModel.list(req.query.account).then((rows)=>{
            // TODO:生成 hls 地址
            let handleRows=[];

            for(var i in rows){
                //TODO: 海康大华的，如果通道大于1，那deviceid和parentid相等就不能用
                // 但JCO的设备，通道等于2时, deviceid和parentid相等是能用的
                if(rows.length>2 && rows[i].deviceid == rows[i].parentid){
                    // nvr 的这个通道不用
                    continue;
                }

                rows[i].hls = Channel.geturl(rows[i].parentid,rows[i].deviceid,'hls');
                rows[i].hlsTls = Channel.geturl(rows[i].parentid,rows[i].deviceid,'hlsTls');
                rows[i].rtmp= Channel.geturl(rows[i].parentid,rows[i].deviceid,'rtmp');
                rows[i].flv = Channel.geturl(rows[i].parentid,rows[i].deviceid,'flv');
                rows[i].flvTls = Channel.geturl(rows[i].parentid,rows[i].deviceid,'flvTls');
                handleRows.push(rows[i]);
            }
            // 处理逻辑 ...
            let ret =Object.assign(constants.httpCode.OK , {data:{items:handleRows,total:handleRows.length}});
            res.send(ret);
        });
    },
    // 发停止播放指令
    bye:function(req,res){
        console.log('bye req',req.query);
        let deviceid = req.query.deviceid;
        let channel  = req.query.channel;
        server.bye(deviceid,channel,(ret)=>{
            if(ret){
                res.send({'code':0,'msg':'success'});
            }else{
                res.send({'code':1,'msg':'通道未注册成功'});
            }
        })
    },
    // 截屏
    snap:function(req,res){
        let deviceid = req.query.deviceid;
        let channel  = req.query.channel;
        let url = Channel.geturl(deviceid,channel,'local');
        
        media.snap(url,deviceid,channel).then((ret)=>{
            
            channelModel.updateSnap(ret.toString(), deviceid,channel).then(()=>{
                res.send(Object.assign(constants.httpCode.OK,{data:{}}));
            }).catch(()=>{
                res.send(Object.assign(constants.httpCode.ILLEGAL_PARAM,{data:{}}));
            })
            
        });
    },
    registry:function(req,res){
        let data = constants.registry;
        res.send(Object.assign(constants.httpCode.OK,{data:data}));
    }
};
module.exports=Channel;
